package com.xxgc.springboot.task;

import lombok.extern.slf4j.Slf4j;
import org.quartz.*;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @program: springboot
 * @description:
 * @author: Mr.abel(ShiJiaYi)
 * @create: 2022-09-19 20:42
 **/
@Slf4j
//有状态每次都会拿到老的jobDataMap
@PersistJobDataAfterExecution
public class HelloJob implements Job {
    //通过构造函数来确定这个对象是否每次都会被创建
    public HelloJob() {
        log.info("HelloJob任务被创建");
    }

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        //得到传递过来的自定义信息
        JobDataMap jobDetailDataMap = context.getJobDetail().getJobDataMap();
        String jobDetailMessage = jobDetailDataMap.getString("受谁指使");
        log.info("任务信息："+jobDetailMessage);
        jobDetailDataMap.put("受谁指使","山鸡");



        //你可在这里调用业务逻辑或者处理数据库等任何操作。
        JobDetail jobDetail = context.getJobDetail();
        String jobDetailName = jobDetail.getKey().getName();
        
        log.info("任务名称："+jobDetailName);
        String jobDetailGroup = jobDetail.getKey().getGroup();
        log.info("任务组名称："+jobDetailGroup);
        String jobSimpleName = jobDetail.getJobClass().getSimpleName();
        log.info("任务实现类名称："+jobSimpleName);

        Trigger trigger = context.getTrigger();
        String triggerName = trigger.getKey().getName();
        log.info("触发器名称："+triggerName);
        String triggerGroup = trigger.getKey().getGroup();
        log.info("触发器组名称："+triggerGroup);

        // 获取其他信息
        Date date = context.getFireTime();
        String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
        log.info("任务执行的开始时间"+format);
        Date nextFireTime = context.getNextFireTime();
        String format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(nextFireTime);
        log.info("下一次任务执行的时间"+format1);
        String string = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        log.info("当前时间："+string);
    }
}
